package com.cskaoyan.service.admin;


import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.*;
import com.cskaoyan.model.admin.bo.BaseParam;
import com.cskaoyan.model.admin.vo.BaseData;
import com.cskaoyan.model.admin.vo.MarketFeedbackList;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author stone
 * @date 2021/11/29 15:17
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    MarketUserMapper userMapper;
    @Autowired
    MarketAddressMapper addressMapper;
    @Autowired
    MarketCollectMapper collectMapper;
    @Autowired
    MarketFootPrintMapper footPrintMapper;
    @Autowired
    MarketSearchHistoryMapper searchHistoryMapper;
    @Autowired
    MarketFeedbackMapper feedbackMapper;


    //例子开头
    @Override
    public BaseData list(String username, String mobile, BaseParam param) {

        //在查询之前使用分页
        PageHelper.startPage(param.getPage(), param.getLimit());

        //List<User> users = userMapper.select(username, mobile, param.getSort(), param.getOrder());

        MarketUserExample example = new MarketUserExample();
        // 👉 List<Criterion> 条件列表
        //Criteria提供的and方法，就是向条件列表里塞入数据
//        example.createCriteria().andUsernameEqualTo(username).andMobileEqualTo(mobile);
        MarketUserExample.Criteria criteria = example.createCriteria();
        if (mobile != null) {
            criteria.andMobileLike("%" + mobile + "%");
        }
        if (username != null) {
            criteria.andUsernameLike("%" + username + "%");
        }

        example.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<MarketUser> users = userMapper.selectByExample(example);

        //long total = userMapper.countByExample(example);
        //通过分页信息拿到分页数据
        //PageInfo的有参构造方法，放入的***查询结果***
        PageInfo info = new PageInfo(users);
        int pages = info.getPages();
        long total = info.getTotal();
        //total == users.size() 👉 false; 如果total小于等于limit是true
        //total ≥ users.size()
        return BaseData.list(users, (int) total, param.getPage(), param.getLimit(), pages);
    }

    //例子结尾
    //通过ID 查找user
    @Override
    public MarketUser userOfID(String id) {

        MarketUserExample example = new MarketUserExample();
        example.createCriteria().andIdEqualTo(Integer.parseInt(id));
        List<MarketUser> marketUsers = userMapper.selectByExample(example);
        MarketUser marketUser = marketUsers.get(0);

        return marketUser;

    }

    //修改用户详情
    @Override
    public int update(MarketUser user) {
        MarketUserExample example = new MarketUserExample();
        example.createCriteria().andIdEqualTo(user.getId());
        int i = userMapper.updateByExampleSelective(user, example);
        return i;
    }

    //收货地址的显示
    @Override
    public BaseData addressList(BaseParam param, String username, String id) {
        //在查询之前使用分页
        PageHelper.startPage(param.getPage(), param.getLimit());

        MarketAddressExample example = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = example.createCriteria();
        if (id != null && id != "") {

            criteria.andUserIdEqualTo(Integer.parseInt(id));
        }
        if (username != null) {
            criteria.andNameLike("%" + username + "%");
        }


        example.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<MarketAddress> users = addressMapper.selectByExample(example);

        //long total = userMapper.countByExample(example);
        //通过分页信息拿到分页数据
        //PageInfo的有参构造方法，放入的***查询结果***
        PageInfo info = new PageInfo(users);
        long total = info.getTotal();
        //total == users.size() 👉 false; 如果total小于等于limit是true
        //total ≥ users.size()
        return BaseData.list(users, (int) total, param.getPage(), param.getLimit(), 1);

    }

    @Override
    public BaseData collectList(BaseParam param, String userId, String valueId) {
        //在查询之前使用分页
        PageHelper.startPage(param.getPage(), param.getLimit());

        MarketCollectExample example = new MarketCollectExample();
        MarketCollectExample.Criteria criteria = example.createCriteria();

        if (valueId != null && valueId != "") {

            criteria.andValueIdEqualTo(Integer.parseInt(valueId));
        }
        if (userId != null && userId != "") {
            criteria.andUserIdEqualTo(Integer.parseInt(userId));
        }


        example.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<MarketCollect> users = collectMapper.selectByExample(example);


        PageInfo info = new PageInfo(users);
        long total = info.getTotal();
        //total == users.size() 👉 false; 如果total小于等于limit是true
        //total ≥ users.size()
        return BaseData.list(users, (int) total, param.getPage(), param.getLimit(), 1);

    }

    @Override
    public BaseData footprintList(BaseParam param, String userId, String valueId) {
        //在查询之前使用分页
        PageHelper.startPage(param.getPage(), param.getLimit());

        MarketFootPrintExample example = new MarketFootPrintExample();
        MarketFootPrintExample.Criteria criteria = example.createCriteria();

        if (valueId != null && valueId != "") {

            criteria.andGoodsIdEqualTo(Integer.parseInt(valueId));
        }
        if (userId != null && userId != "") {
            criteria.andUserIdEqualTo(Integer.parseInt(userId));
        }


        example.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<MarketFootPrint> users = footPrintMapper.selectByExample(example);


        PageInfo info = new PageInfo(users);
        long total = info.getTotal();
        //total == users.size() 👉 false; 如果total小于等于limit是true
        //total ≥ users.size()
        return BaseData.list(users, (int) total, param.getPage(), param.getLimit(), 1);

    }

    @Override
    public BaseData historyList(BaseParam param, String userId, String keyword) {
        //在查询之前使用分页
        PageHelper.startPage(param.getPage(), param.getLimit());

        MarketSearchHistoryExample example = new MarketSearchHistoryExample();
        MarketSearchHistoryExample.Criteria criteria = example.createCriteria();

        if (keyword != null) {

            criteria.andKeywordLike("%" + keyword + "%");
        }
        if (userId != null && userId != "") {
            criteria.andUserIdEqualTo(Integer.parseInt(userId));
        }
        example.setOrderByClause(param.getSort() + " " + param.getOrder());
        List<MarketSearchHistory> users = searchHistoryMapper.selectByExample(example);


        PageInfo info = new PageInfo(users);
        long total = info.getTotal();
        //total == users.size() 👉 false; 如果total小于等于limit是true
        //total ≥ users.size()
        return BaseData.list(users, (int) total, param.getPage(), param.getLimit(), 1);

    }

    @Override
    public BaseData feedbackList(BaseParam param, String id, String username) {
        //在查询之前使用分页
        PageHelper.startPage(param.getPage(), param.getLimit());

        MarketFeedbackExample example = new MarketFeedbackExample();
        MarketFeedbackExample.Criteria criteria = example.createCriteria();

        if (username != null) {

            criteria.andUsernameLike("%" + username + "%");
        }
        if (id != null && id.matches("^[0-9]*$")) {

            criteria.andUserIdEqualTo(Integer.parseInt(id));
        }
        //排序的
        example.setOrderByClause(param.getSort() + " " + param.getOrder());

        List<MarketFeedback> users = feedbackMapper.selectByExample(example);
        MarketFeedbackList marketFeedbackList = new MarketFeedbackList();
        List<MarketFeedbackList> user2=new ArrayList<>();
        for (MarketFeedback user : users) {
            try {
                BeanUtils.copyProperties(marketFeedbackList,user);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                String[] strings = objectMapper.readValue(user.getPicUrls(), String[].class);
                marketFeedbackList.setPicUrls(strings);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            user2.add(marketFeedbackList);
        }

        PageInfo info = new PageInfo(users);
        long total = info.getTotal();
        //total == users.size() 👉 false; 如果total小于等于limit是true
        //total ≥ users.size()
        return BaseData.list(user2, (int) total, param.getPage(), param.getLimit(), 1);

    }
}